package com.xuanyuan.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.xuanyuan.dao.UserDao;
import com.xuanyuan.model.Departments;
import com.xuanyuan.model.User;
import com.xuanyuan.util.PageInfo;

@Component("userDao")
public class UserDaoImpl extends HibernateDaoImpl implements UserDao{
	

	public Boolean isLogin(String userName, String password) {
		String hql = "from User u where u.userName='"+userName+"' and u.password='"+password+"'";
		List<User> list = this.find(hql);
		System.out.println(list.size());
		if(list.size()>0){
			return true;
		}else{
			return false;
		}
	}

	public void addUser(User user) {
		System.out.println("User-->"+user);
		this.save(user);
	}

	public List<User> listUser(PageInfo pageInfo) {
		String hql = "from User";
		System.out.println(hql);
		System.out.println("pageInfo:"+pageInfo);
		if(pageInfo == null){
			return this.find(hql);
		} else {
			 List list = this.findPageByQuery(hql, null, pageInfo);
			 return list;
		}
		
	}

	public User selectUser(String userName) {
		String hql = "from User u where u.userName='"+userName+"'";
		List<User> l = this.find(hql);
		if(l.size()>0){
			return l.get(0);
		} else {
			return null;
		}
	}

	public void updatePass(Integer userId, String password) {
		String hql = "update User u set u.password=? where u.id=?";
		Object object[] = new Object[]{password,userId};
		this.updateByQuery(hql, object);
	}

	public void updateUser(Integer userId, String userName, String name,
			String department, String phone, String email) {
		String hql = "update User u set u.userName=?, u.name=?, u.department=?, u.phone=?, u.email=? where u.id=?";
		Object[] objects = new Object[]{userName, name, department, phone, email, userId};
		this.updateByQuery(hql, objects);
	}

	public void delUser(User user) {
		this.delete(user);
	}

	public User getUsers(Integer userId) {
		String hql = "from User u where u.id="+userId;
		return (User)this.find(hql).get(0);
	}

	public void addUserToDepartment(Departments depar) {
		this.save(depar);
	}

	public void updatePass(String password, String username) {
        String hql = "update User u set u.password=? where u.userName=?";
        Object[] o = new Object[]{password, username};
        this.updateByQuery(hql, o);
	}

	public User selectPass(String password, String username) {
		String hql = "from User s where s.password='"+password+"' and s.userName='"+username+"'";
		List list = this.find(hql);
		System.out.println("list-->"+list.size());
		if(list.size()< 1){
			return null;
		} else {
			return (User) list.get(0);
		}
	}

	public List<User> getListUser(String username, PageInfo pageInfo) {
		String hql = "from User s where s.userName like '%"+username+"%'";
		if(pageInfo == null){
			return this.find(hql);
		} else {
			List<User> list = this.findPageByQuery(hql, null, pageInfo);
			System.out.println(list);
			return list;
		}
	}



	
	
	

}
